﻿Imports System.Data.SqlClient

Public Class Login
    Dim ldaplogin As New LdapAuthentication

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub txtpassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpassword.KeyPress
        If e.KeyChar = Chr(13) Then
            UserValidation()
        End If
    End Sub

    Private Sub btnenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenter.Click
        UserValidation()
    End Sub

    Private Sub UserValidation()
        If CheckBox1.Checked = True Then
            If Base_Login(txtuser.Text.Trim) = True And LDAP(txtuser.Text.Trim, txtpassword.Text.Trim) = True Then
                txtpassword.Text = ""
                txtpassword.Focus()
                Config.ShowDialog()
                Me.Close()
            Else
                lblmessage.Text = "Datos incorrectos"
                txtpassword.Text = ""
                txtpassword.Focus()
            End If
        Else
            If Base_Login(txtuser.Text.Trim, txtpassword.Text.Trim) Then
                txtpassword.Text = ""
                txtpassword.Focus()
                'Me.Hide()
                Config.ShowDialog()
                Me.Close()
            Else
                lblmessage.Text = "Datos incorrectos"
                txtpassword.Text = ""
                txtpassword.Focus()
            End If
        End If
    End Sub

    Protected Function LDAP(ByVal user As String, ByVal password As String) As Boolean
        Dim Resultado As Boolean
        Resultado = ldaplogin.ValidateActiveDirectoryLogin("AM", user, password)
        Return Resultado
    End Function

    Protected Function Base_Login(ByVal user As String) As Boolean
        Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
        Dim cmd As New SqlCommand("SELECT COUNT(*) FROM Usuarios WHERE (usuario = @user)", cnn)
        cmd.Parameters.AddWithValue("@user", user)
        cnn.Open()
        If CInt(cmd.ExecuteScalar) >= 1 Then
            Return True
        Else
            Return False
        End If
    End Function

    Protected Function Base_Login(ByVal user As String, ByVal pass As String) As Boolean
        Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
        Dim cmd As New SqlCommand("SELECT COUNT(*) FROM Usuarios WHERE (usuario = @user and password = @pass)", cnn)
        cmd.Parameters.AddWithValue("@user", user)
        cmd.Parameters.AddWithValue("@pass", pass)
        cnn.Open()
        If CInt(cmd.ExecuteScalar) >= 1 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblmessage.Text = ""
        txtuser.Focus()
    End Sub

End Class